package com.markerhub.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.markerhub.entity.SysCourse;
import com.markerhub.entity.SysSemester;
import com.markerhub.entity.SysSemesterCourse;
import com.markerhub.mapper.SysSemesterCourseMapper;
import com.markerhub.service.SysSemesterCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.markerhub.service.SysSemesterService;
import io.jsonwebtoken.impl.crypto.MacProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wang
 * @since 2022-04-27
 */
@Service
public class SysSemesterCourseServiceImpl extends ServiceImpl<SysSemesterCourseMapper, SysSemesterCourse> implements SysSemesterCourseService {

    @Autowired
    SysSemesterService sysSemesterService;

    @Override
    public Map<Long,String> semesterMap(SysCourse sysCourse){
        Map<Long,String> semester= new TreeMap<>();
        List <SysSemesterCourse> semesterId = this.list(new QueryWrapper<SysSemesterCourse>().eq("course_id",sysCourse.getId()));
        for(SysSemesterCourse m:semesterId){
            String season = sysSemesterService.getOne(new QueryWrapper<SysSemester>().eq("id", m.getSemesterId())).getSeason();
            semester.put(m.getSemesterId(),season);
        }
        return semester;
    }

    @Override
    public Boolean saveSemester(Long courseId,Map<Long,String> semester){
        if(semester==null)return false;
        Set<Long> semesterId = semester.keySet();
        for (Long i:semesterId){
            SysSemesterCourse sysSemesterCourse = new SysSemesterCourse();
            sysSemesterCourse.setSemesterId(i);
            sysSemesterCourse.setCourseId(courseId);
            if(!this.save(sysSemesterCourse))return false;
        }
        return true;
    }

}
